package com.kexio.test;

import java.util.Collection;

import com.kexio.auth.annotation.RequiresPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kexio.common.dto.Result;

/**
 * CacheManager 诊断控制器
 * 
 * 用于检查当前使用的 CacheManager 类型
 */
@RestController
@RequestMapping("/test/cache-manager")
public class CacheManagerDiagnosticController {
    
    @Autowired
    private ApplicationContext applicationContext;
    
    @Autowired
    private CacheManager cacheManager;
    
    /**
     * 检查当前使用的 CacheManager
     */
    @GetMapping("/current")
    // ⚠️ 生产环境安全：移除@SaIgnore，增加权限控制
    @RequiresPermission("system:test:view")
    public Result<Object> getCurrentCacheManager() {
        StringBuilder info = new StringBuilder();
        
        // 当前默认 CacheManager
        info.append("默认 CacheManager: ").append(cacheManager.getClass().getSimpleName()).append("\n");
        info.append("CacheManager 详细类型: ").append(cacheManager.getClass().getName()).append("\n");
        
        // 获取所有 CacheManager Bean
        info.append("\n所有 CacheManager Bean:\n");
        String[] cacheManagerNames = applicationContext.getBeanNamesForType(CacheManager.class);
        for (String name : cacheManagerNames) {
            CacheManager cm = applicationContext.getBean(name, CacheManager.class);
            info.append("  - ").append(name).append(": ").append(cm.getClass().getSimpleName()).append("\n");
        }
        
        // 获取当前缓存名称
        Collection<String> cacheNames = cacheManager.getCacheNames();
        info.append("\n当前缓存名称: ").append(cacheNames).append("\n");
        
        return Result.success("CacheManager 诊断信息", info.toString());
    }
    
    /**
     * 检查 test-cache 缓存的实际类型
     */
    @GetMapping("/test-cache-type")
    // ⚠️ 生产环境安全：移除@SaIgnore，增加权限控制
    @RequiresPermission("system:test:view")
    public Result<Object> getTestCacheType() {
        try {
            var cache = cacheManager.getCache("test-cache");
            if (cache == null) {
                return Result.success("缓存信息", "test-cache 不存在");
            }
            
            StringBuilder info = new StringBuilder();
            info.append("test-cache 类型: ").append(cache.getClass().getSimpleName()).append("\n");
            info.append("test-cache 详细类型: ").append(cache.getClass().getName()).append("\n");
            info.append("底层缓存类型: ").append(cache.getNativeCache().getClass().getSimpleName()).append("\n");
            info.append("底层缓存详细类型: ").append(cache.getNativeCache().getClass().getName()).append("\n");
            
            return Result.success("test-cache 详细信息", info.toString());
            
        } catch (Exception e) {
            return Result.error("获取缓存信息失败: " + e.getMessage());
        }
    }
}
